package org.dromara.exam.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.dromara.exam.domain.bo.ExamWordMeaningBo;
import org.dromara.exam.domain.vo.ExamWordMeaningVo;
import org.dromara.exam.domain.ExamWordMeaning;
import org.dromara.exam.mapper.ExamWordMeaningMapper;
import org.dromara.exam.service.IExamWordMeaningService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 单词释义表Service业务层处理
 *
 * @author Lion Li
 * @date 2025-09-27
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ExamWordMeaningServiceImpl implements IExamWordMeaningService {

    private final ExamWordMeaningMapper baseMapper;

    /**
     * 查询单词释义表
     *
     * @param meaningId 主键
     * @return 单词释义表
     */
    @Override
    public ExamWordMeaningVo queryById(Long meaningId){
        return baseMapper.selectVoById(meaningId);
    }

    /**
     * 分页查询单词释义表列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 单词释义表分页列表
     */
    @Override
    public TableDataInfo<ExamWordMeaningVo> queryPageList(ExamWordMeaningBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ExamWordMeaning> lqw = buildQueryWrapper(bo);
        Page<ExamWordMeaningVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的单词释义表列表
     *
     * @param bo 查询条件
     * @return 单词释义表列表
     */
    @Override
    public List<ExamWordMeaningVo> queryList(ExamWordMeaningBo bo) {
        LambdaQueryWrapper<ExamWordMeaning> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ExamWordMeaning> buildQueryWrapper(ExamWordMeaningBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ExamWordMeaning> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(ExamWordMeaning::getMeaningId);
        lqw.eq(bo.getWordId() != null, ExamWordMeaning::getWordId, bo.getWordId());
        lqw.eq(StringUtils.isNotBlank(bo.getMeaning()), ExamWordMeaning::getMeaning, bo.getMeaning());
        lqw.eq(bo.getUserId() != null, ExamWordMeaning::getUserId, bo.getUserId());
        lqw.eq(bo.getIsDefault() != null, ExamWordMeaning::getIsDefault, bo.getIsDefault());
        return lqw;
    }

    /**
     * 新增单词释义表
     *
     * @param bo 单词释义表
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(ExamWordMeaningBo bo) {
        ExamWordMeaning add = MapstructUtils.convert(bo, ExamWordMeaning.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setMeaningId(add.getMeaningId());
        }
        return flag;
    }

    /**
     * 修改单词释义表
     *
     * @param bo 单词释义表
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(ExamWordMeaningBo bo) {
        ExamWordMeaning update = MapstructUtils.convert(bo, ExamWordMeaning.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ExamWordMeaning entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除单词释义表信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
